package com.atguigu.guli.service.eduucenter.service.impl;

import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.eduucenter.entity.RegisterVo;
import com.atguigu.guli.service.eduucenter.entity.UcenterMember;
import com.atguigu.guli.service.eduucenter.mapper.UcenterMemberMapper;
import com.atguigu.guli.service.eduucenter.service.UcenterMemberService;
import com.atguigu.guli.service.utils.JwtUtils;
import com.atguigu.guli.service.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-06-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {
        String password = ucenterMember.getPassword();
        String encrypt = MD5.encrypt(password);
        String mobile = ucenterMember.getMobile();
        if (StringUtils.isEmpty(password)||StringUtils.isEmpty(mobile)){
            throw new GuliException(20001,"登录失败");
        }
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,mobile);
        UcenterMember selectOne = baseMapper.selectOne(queryWrapper);
        if (selectOne == null){
            throw new GuliException(20001,"登录失败");
        }

        if (!encrypt.equals(selectOne.getPassword()))
        {
            throw new GuliException(20001,"登录失败");
        }

        if (selectOne.getDisabled()){
            throw new GuliException(20001,"登录失败");
        }

        String jwtToken = JwtUtils.getJwtToken(selectOne.getId(), selectOne.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String encrypt = MD5.encrypt(password);

        String key = redisTemplate.opsForValue().get(mobile);
        if (!key.equals(code)){
            throw  new GuliException(20001,"验证码不正确");
        }

        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (integer>0){
            throw  new GuliException(20001,"手机号已存在");
        }

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(encrypt);
        ucenterMember.setDisabled(false);
        ucenterMember.setAvatar("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1115%2F101021113337%2F211010113337-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657379596&t=5e97acb1cf72366fc19a87b9239c7f47");

        this.save(ucenterMember);

    }
}
